// globalData---》全局树形数据，存放增加，删除，修改后的树形数据
var globalData = []
    // SQstatus---》收起状态标记值
var SQstatus = false
    // ZKstatus---》展开状态标记值
var ZKstatus = false
    // currentGlobalData---》当前编辑缓存
var currentGlobalData = {}
    /* 
      formatData 函数：数据转换，将一维数组数据转换为树形数据
      参数：data---》原始一维数组数据
      返回值：resultArr---》格式化好的树形数据
    */
function formatData(data) {
    let resultArr = []
    for (let i = 0; i < data.length; i++) {
        if (data[i].menuParentid == '') {
            resultArr.push(data[i])
        }
    }
    for (let i = 0; i < resultArr.length; i++) {
        let newArr = []
        for (let j = 0; j < data.length; j++) {
            if (data[j].menuParentid == resultArr[i].menuId) {
                newArr.push(data[j])
            }
        }
        resultArr[i].children = newArr
    }
    for (let i = 0; i < resultArr.length; i++) {
        for (let k = 0; k < resultArr[i].children.length; k++) {
            let newArr1 = []
            for (let j = 0; j < data.length; j++) {
                if (data[j].menuParentid == resultArr[i].children[k].menuId) {
                    newArr1.push(data[j])
                }
            }
            resultArr[i].children[k].children = newArr1
        }
    }
    return resultArr
}
    
/* 
  initTable 函数: 初始化表格
  参数: data---》转换的树形数据
*/
function initTable(data) {
    globalData = JSON.parse(JSON.stringify(data))
    var tableEL = document.createElement('table')
    var containerEL = document.getElementById('container')
    containerEL.appendChild(tableEL)
    tableEL.appendChild(createTH())
    for (var i = 0; i < data.length; i++) {
        if (data[i].menuType == '菜单') {
            var trEL = createTR(data[i], 1, (i + 1) + '', 1)
        } else {
            var trEL = createTR(data[i], 0, (i + 1) + '', 1)
        }
        trEL.style.backgroundColor = '#606266'
        tableEL.appendChild(trEL)
        for (var j = 0; j < data[i].children.length; j++) {
            if (data[i].children[j].menuType == '菜单') {
                var trEL = createTR(data[i].children[j], 1, (i + 1) + '-' + (j + 1), 2)
            } else {
                var trEL = createTR(data[i].children[j], 0, (i + 1) + '-' + (j + 1), 2)
            }
            trEL.style.backgroundColor = '#909399'
            tableEL.appendChild(trEL)
            for (var k = 0; k < data[i].children[j].children.length; k++) {
                if (data[i].children[j].children[k].menuType == '菜单') {
                    var trEL = createTR(data[i].children[j].children[k], 1, (i + 1) + '-' + (j + 1) + '-' + (k + 1), 3)
                    trEL.children[0].children[0].style.marginLeft = '60px'
                    trEL.children[0].children[0].disabled = 'true'
                    trEL.children[0].children[0].style.cursor = 'not-allowed'
                    trEL.children[0].children[1].disabled = 'true'
                    trEL.children[0].children[1].style.cursor = 'not-allowed'
                    trEL.children[6].children[2].disabled = 'true'
                    trEL.children[6].children[2].style.cursor = 'not-allowed'
                } else {
                    var trEL = createTR(data[i].children[j].children[k], 0, (i + 1) + '-' + (j + 1) + '-' + (k + 1), 3)
                }
                trEL.style.backgroundColor = '#C0C4CC'
                tableEL.appendChild(trEL)
            }
        }
    }
}
/*
  createTH 函数: 创建表头
  返回值: trEL---》表头元素
*/
function createTH() {
    var trEL = document.createElement('tr')
    var thEL0 = document.createElement('th')
    thEL0.innerHTML = '序号'
    trEL.appendChild(thEL0)
    var thEL1 = document.createElement('th')
    thEL1.innerHTML = '功能名称'
    trEL.appendChild(thEL1)
    var thEL2 = document.createElement('th')
    thEL2.innerHTML = '是否启用'
    trEL.appendChild(thEL2)
    var thEL3 = document.createElement('th')
    thEL3.innerHTML = '功能类型'
    trEL.appendChild(thEL3)
    var thEL4 = document.createElement('th')
    thEL4.innerHTML = '创建时间'
    trEL.appendChild(thEL4)
    var thEL5 = document.createElement('th')
    thEL5.innerHTML = '功能备注'
    trEL.appendChild(thEL5)
    var thEL6 = document.createElement('th')
    thEL6.innerHTML = '相关操作'
    trEL.appendChild(thEL6)
    return trEL
}
/* 
  createTR 函数: 创建每一列
  参数1: currentData---》当前列的数据
  参数2: type---》类型，决定有没有展开折叠按钮，1 有，其他无
  参数3 index---》计算的当前列的标号
  参数4: tier---》第几层
  返回值: trEL---》创建的当前 tr
*/
function createTR(currentData, type, index, tier) {
    var trEL = document.createElement('tr')
    if (type == 1) {
        var tdEL0 = document.createElement('td')
        tdEL0.innerHTML = index
        tdEL0.style.fontWeight = 'bold'
        tdEL0.style.color = 'white'
        trEL.appendChild(tdEL0)
        var spanEL = document.createElement('span')
        spanEL.classList.add('span')
        spanEL.title = '展开'
        spanEL.onclick = clickZK
        if (tier == 2) {
            spanEL.style.marginLeft = '30px'
        }
        var spanEL1 = document.createElement('span')
        spanEL1.classList.add('span1')
        spanEL1.title = '收起'
        spanEL1.onclick = clickSQ
        var tdEL1 = document.createElement('td')
        tdEL1.innerHTML = currentData.menuText
        tdEL0.style.textAlign = 'left'
        tdEL0.appendChild(spanEL)
        tdEL0.appendChild(spanEL1)
        trEL.appendChild(tdEL1)
        var tdEL2 = document.createElement('td')
        tdEL2.innerHTML = currentData.menuIsmodify
        trEL.appendChild(tdEL2)
        var tdEL3 = document.createElement('td')
        tdEL3.innerHTML = currentData.menuType
        currentData.menuType == '菜单' ? tdEL3.style.color = '#409EFF' : tdEL3.style.color = 'green'
        trEL.appendChild(tdEL3)
        var tdEL4 = document.createElement('td')
        tdEL4.innerHTML = datefomate(currentData.menuCreatetime)
        trEL.appendChild(tdEL4)
        var tdEL5 = document.createElement('td')
        tdEL5.innerHTML = currentData.menuNote
        trEL.appendChild(tdEL5)
        var tdEL6 = document.createElement('td')
        var editBtn = document.createElement('button')
        editBtn.innerText = '编辑'
        editBtn.classList.add('button1')
        editBtn.onclick = editTableData
        tdEL6.appendChild(editBtn)
        var delBtn = document.createElement('button')
        delBtn.innerText = '删除'
        delBtn.classList.add('button2')
        delBtn.onclick = delTableData
        tdEL6.appendChild(delBtn)
        var addBtn = document.createElement('button')
        addBtn.innerText = '新增'
        addBtn.onclick = addTableData
        tdEL6.appendChild(addBtn)
        addBtn.classList.add('button0')
        trEL.appendChild(tdEL6)
    } else {
        var tdEL0 = document.createElement('td')
        var strongEL = document.createElement('strong')
        strongEL.innerHTML = index
        tdEL0.appendChild(strongEL)
        tdEL0.style.textAlign = 'left'
        if (tier == 2) {
            strongEL.style.marginLeft = '40px'
        } else {
            strongEL.style.marginLeft = '60px'
        }
        trEL.appendChild(tdEL0)
        var tdEL1 = document.createElement('td')
        tdEL1.innerHTML = currentData.menuText
        trEL.appendChild(tdEL1)
        var tdEL2 = document.createElement('td')
        tdEL2.innerHTML = currentData.menuIsmodify
        trEL.appendChild(tdEL2)
        var tdEL3 = document.createElement('td')
        tdEL3.innerHTML = currentData.menuType
        currentData.menuType == '菜单' ? tdEL3.style.color = '#409EFF' : tdEL3.style.color = 'green'
        trEL.appendChild(tdEL3)
        var tdEL4 = document.createElement('td')
        tdEL4.innerHTML = datefomate(currentData.menuCreatetime)
        trEL.appendChild(tdEL4)
        var tdEL5 = document.createElement('td')
        tdEL5.innerHTML = currentData.menuNote
        trEL.appendChild(tdEL5)
        var tdEL6 = document.createElement('td')
        var editBtn = document.createElement('button')
        editBtn.innerText = '编辑'
        editBtn.classList.add('button1')
        editBtn.onclick = editTableData
        tdEL6.appendChild(editBtn)
        var delBtn = document.createElement('button')
        delBtn.innerText = '删除'
        delBtn.classList.add('button2')
        delBtn.onclick = delTableData
        tdEL6.appendChild(delBtn)
        trEL.appendChild(tdEL6)
    }
    return trEL
}
// clickSQ 函数: 列收起
function clickSQ() {
    initData = JSON.parse(JSON.stringify(globalData))
    var currentText = this.parentNode.parentNode.children[1].innerText
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            globalData[i].children = []
            document.getElementById('container').innerHTML = ''
            initTable(globalData)
            SQstatus = true
            break
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                if (globalData[i].children[j].menuText == currentText) {
                    globalData[i].children[j].children = []
                    document.getElementById('container').innerHTML = ''
                    initTable(globalData)
                    break
                }
            }
        }
    }
    SQstatus = false
}
// clickZK 函数: 列展开
function clickZK() {
    document.getElementById('container').innerHTML = ''
    initTable(initData)
}
// delTableData函数: 删除操作
function delTableData() {
    var currentText = this.parentNode.parentNode.children[1].innerText
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            globalData.splice(i, 1)
            document.getElementById('container').innerHTML = ''
            initTable(globalData)
            SQstatus = true
            break
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                if (globalData[i].children[j].menuText == currentText) {
                    globalData[i].children.splice(j, 1)
                    document.getElementById('container').innerHTML = ''
                    initTable(globalData)
                    SQstatus = true
                    break
                }
            }
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                for (var k = 0; k < globalData[i].children[j].children.length; k++) {
                    if (globalData[i].children[j].children[k].menuText == currentText) {
                        globalData[i].children[j].children.splice(k, 1)
                        document.getElementById('container').innerHTML = ''
                        initTable(globalData)
                        break
                    }
                }
            }
        }
    }
    SQstatus = false
}
// editTableData 函数: 编辑操作
function editTableData() {
    document.getElementById('edit').style.display = 'block'
    var currentText = this.parentNode.parentNode.children[1].innerText
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            setEditData(globalData[i])
            currentGlobalData = globalData[i]
            SQstatus = true
            break
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                if (globalData[i].children[j].menuText == currentText) {
                    setEditData(globalData[i].children[j])
                    currentGlobalData = globalData[i].children[j]
                    break
                }
            }
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                for (var k = 0; k < globalData[i].children[j].children.length; k++) {
                    if (globalData[i].children[j].children[k].menuText == currentText) {
                        setEditData(globalData[i].children[j].children[k])
                        currentGlobalData = globalData[i].children[j].children[k]
                        break
                    }
                }
            }
        }
    }
    SQstatus = false
}
/* 
  etEditData 函数: 编辑数据回显
  参数: data---》编辑回显的当前数据
*/
function setEditData(data) {
    document.getElementById('menuText').value = data.menuText
    document.getElementById('menuIsmodify').value = data.menuIsmodify == '启用' ? 1 : 0
    document.getElementById('menuType').value = data.menuType == '菜单' ? 1 : 0
    document.getElementById('menuCreatetime').value = datefomate(data.menuCreatetime)
    document.getElementById('menuNote').value = data.menuNote
}
// 编辑关闭
document.getElementById('editClose').onclick = function() {
        document.getElementById('edit').style.display = 'none'
    }
    // 编辑保存
document.getElementById('editSave').onclick = function() {
        currentGlobalData.menuText = document.getElementById('menuText').value
        currentGlobalData.menuIsmodify = document.getElementById('menuIsmodify').value == 1 ? '启用' : '禁用'
        currentGlobalData.menuType = document.getElementById('menuType').value == 1 ? '菜单' : '链接'
        currentGlobalData.menuCreatetime = document.getElementById('menuCreatetime').value
        currentGlobalData.menuNote = document.getElementById('menuNote').value
        document.getElementById('container').innerHTML = ''
        initTable(globalData)
        document.getElementById('edit').style.display = 'none'
    }
    // 编辑取消
document.getElementById('editCancle').onclick = function() {
        document.getElementById('edit').style.display = 'none'
    }
    // addTableData 函数：新增操作
function addTableData() {
    document.getElementById('add').style.display = 'block'
    document.getElementById('menuCreatetime1').value = datefomate(Date.now())
    var currentText = this.parentNode.parentNode.children[1].innerText
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            currentGlobalData = globalData[i]
            SQstatus = true
            break
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                if (globalData[i].children[j].menuText == currentText) {
                    currentGlobalData = globalData[i].children[j]
                    break
                }
            }
        }
    }
    if (SQstatus == false) {
        for (var i = 0; i < globalData.length; i++) {
            for (var j = 0; j < globalData[i].children.length; j++) {
                for (var k = 0; k < globalData[i].children[j].children.length; k++) {
                    if (globalData[i].children[j].children[k].menuText == currentText) {
                        currentGlobalData = globalData[i].children[j].children[k]
                        break
                    }
                }
            }
        }
    }
    SQstatus = false
}
// 新增关闭
document.getElementById('addClose').onclick = function() {
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    //新增保存
document.getElementById('addSave').onclick = function() {
        var obj = {}
        obj.children = []
        obj.menuText = document.getElementById('menuText1').value
        obj.menuIsmodify = document.getElementById('menuIsmodify1').value == 1 ? '启用' : '禁用'
        obj.menuType = document.getElementById('menuType1').value == 1 ? '菜单' : '链接'
        obj.menuCreatetime = document.getElementById('menuCreatetime1').value
        obj.menuNote = document.getElementById('menuNote1').value
        currentGlobalData.children.push(obj)
        document.getElementById('container').innerHTML = ''
        initTable(globalData)
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    // 新增取消
document.getElementById('addCancle').onclick = function() {
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    // datefomate函数: 格式化日期
function datefomate(value) {
    if (value == null || value == undefined) {
        return "";
    }
    var date = new Date(value);

    Y = date.getFullYear(),
        m = date.getMonth() + 1,
        d = date.getDate(),
        H = date.getHours(),
        i = date.getMinutes(),
        s = date.getSeconds();
    return Y + '-' + m + '-' + d + '    ' + H + ':' + i + ':' + s;
};